﻿namespace KLF.Lib.Browser.DOM.Events
{
    /// <summary>
    /// The MutationEvent interface provides specific contextual
    /// information associated with Mutation events.
    /// </summary>
    public interface IMutationEvent : IEvent
    {
        int attrChange { get; }

        string attrName { get; }

        void initMutationEvent(string typeArg, bool canBuubbleArg, bool cancelableArg, 
            Node relatedNodeArg, string prevValueArg, string newValueArg, string attrNameArg, int attrChangeArg);

        string newValue { get; }

        string prevValue { get; }

        Node relatedNode { get; }
    }

    /// <summary>
    /// The MutationEvent interface provides specific contextual
    /// information associated with Mutation events.
    /// </summary>
    public class MutationEvent : Event
    {
        #region Constants

        public const short MODICICATION = 1;
        public const short ADDITION = 2;
        public const short REMOVAL = 3;

        #endregion

        #region Constructors

        internal MutationEvent(string _type)
        {
            type = _type;

            switch (_type.ToLowerInvariant())
            {
                case "domsubtreemodified":
                case "domnodeinserted":
                case "domnoderemoved":
                case "domcharacterdatamodified":
                case "donattrmodified":
                    bubbles = true;
                    cancelable = false;
                    break;

                case "domnoderemovedfromdocument":
                case "domnodeinsertedintodocument":
                    bubbles = false;
                    cancelable = false;
                    break;
            }
        }

        #endregion

        #region DOM Level 2 Events Compliance

        public int attrChange
        {
            get;
            internal set;
        }

        public string attrName
        {
            get;
            internal set;
        }

        public void initMutationEvent(string typeArg, bool canBuubbleArg, bool cancelableArg, 
            Node relatedNodeArg, string prevValueArg, string newValueArg, string attrNameArg, int attrChangeArg)
        {
            if (!dispatched)
            {
                base.initEvent(typeArg, canBuubbleArg, cancelableArg);

                attrName = attrNameArg;
                attrChange = attrChangeArg;
                relatedNode = relatedNodeArg;
                newValue = newValueArg;
                prevValue = prevValueArg;
            }
        }

        public string newValue
        {
            get;
            internal set;
        }

        public string prevValue
        {
            get;
            internal set;
        }

        public Node relatedNode
        {
            get;
            internal set;
        }

        #endregion
    }
}
